package com.huawei.hms.framework.network.restclient;

import android.content.Context;
import com.huawei.hms.framework.common.Logger;
import com.huawei.hms.framework.network.restclient.Response.Builder;
import com.huawei.hms.framework.network.restclient.hwhttp.Callback;
import com.huawei.hms.framework.network.restclient.hwhttp.HttpClientGlobalInstance;
import com.huawei.hms.framework.network.restclient.hwhttp.Response;
import com.huawei.hms.framework.network.restclient.hwhttp.Submit;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.DNManager;
import com.huawei.hms.framework.network.restclient.hwhttp.okhttp.OkHttpClientGlobal;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

public class RestClientGlobalInstance {
    private static final String TAG = RestClientGlobalInstance.class.getSimpleName();
    private static RestClientGlobalInstance instance = new RestClientGlobalInstance();
    private DNManager dnManager = DNManager.getInstance();
    private RestClient restClient;

    private RestClientGlobalInstance() {
    }

    public static RestClientGlobalInstance getInstance() {
        return instance;
    }

    public RestClientGlobalInstance init(Context context) {
        HttpClientGlobalInstance.getInstance().init(context);
        return this;
    }

    public RestClientGlobalInstance setNetSDKType(String str) {
        HttpClientGlobalInstance.getInstance().setNetSDKType(str);
        return this;
    }

    public RestClientGlobalInstance initConnectionPool(int i, long j, TimeUnit timeUnit) {
        if (i > 0 && j > 0) {
            OkHttpClientGlobal.init(i, j, timeUnit);
        }
        return this;
    }

    public void connect(String str, int i, ResultCallback<Void> resultCallback) {
        connect(getRestClient(), str, i, resultCallback);
    }

    public void connect(RestClient restClient, String str, int i, final ResultCallback<Void> resultCallback) {
        if (resultCallback == null) {
            Logger.w(TAG, "callback is null");
        }
        if (restClient == null || restClient.getHttpClient() == null) {
            Logger.w(TAG, "restClient is null or restClient.getHttpClient() is null");
        } else {
            HttpClientGlobalInstance.getInstance().connect(restClient.getHttpClient(), str, i, new Callback() {
                public void onResponse(Submit submit, Response response) throws IOException {
                    if (resultCallback == null) {
                        response.close();
                        return;
                    }
                    Builder message = new Builder().headers(response.getHeaders()).code(response.getCode()).message(response.getMessage());
                    if (response.isSuccessful()) {
                        response.close();
                    } else {
                        message.errorBody(response.getBody().bytes());
                    }
                    resultCallback.onResponse(message.build());
                }

                public void onFailure(Submit submit, Throwable th) {
                    if (resultCallback != null) {
                        resultCallback.onFailure(th);
                    }
                }
            });
        }
    }

    public synchronized RestClient getRestClient() {
        if (this.restClient == null) {
            this.restClient = new RestClient.Builder().httpClient(HttpClientGlobalInstance.getInstance().getHttpClient()).addConverterFactory(new ToStringConverterFactory()).build();
        }
        return this.restClient;
    }

    public DNManager getDnManager() {
        return this.dnManager;
    }
}
